//
//  WBCommonDefine.h
//  WeatherBar
//
//  Created by yby on 2024/6/7.
//

#ifndef WBCommonDefine_h
#define WBCommonDefine_h



/** 弱指针 */
#define WeakSelf(weakSelf)                         __weak __typeof(&*self)weakSelf = self;   //weakSelf
// 自定义Log
#ifdef DEBUG
#define NSLog(...) NSLog(__VA_ARGS__)
#else
#define NSLog(...)
#endif

#ifdef DEBUG

#define NFRLog(format, ...) printf("class: <%p %s:(%d) > method: %s \n%s\n", self, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(format), ##__VA_ARGS__] UTF8String] )

#else

#define NFRLog(...)

#endif


#define kLineH (1 / [UIScreen mainScreen].scale)
#define kLineOffset  (kLineNeedOffset ? 0 : ((1 / [UIScreen mainScreen].scale) / 2))
#define kLineNeedOffset ((int)[UIScreen mainScreen].scale % 2 == 0)


#define UIScreenWidth                              [UIScreen mainScreen].bounds.size.width
#define UIScreenHeight                             [UIScreen mainScreen].bounds.size.height


#pragma mark - UIColor宏定义
#define UIColorFromRGBA(rgbValue, alphaValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:alphaValue]



//判断字符串是否为空
#define beNil(aString) (![aString isKindOfClass:[NSString class]]||[aString isKindOfClass:[NSNull class]]|| aString==nil||((NSString *)aString).length == 0||[aString stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0)

//判断字符串为空并返回空字符串 suitable checked
#define returnBeNil(aString)\
({\
NSString *resultStr = beNil(aString) ? @"" : aString;\
(resultStr);\
})\

//空检测
#define isEmpty(string) ((string) == nil || (string) == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [(string) isEqual:@""] || [(string) isEqual:@" "])


/// table分页数量
#define kPageOfRows @"10"
#define IntToString(value) [NSString stringWithFormat:@"%ld",(long)value]


#pragma mark 判断系统版本------------------
#define iOS9                         ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)
// 获取系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
// 应用版本号
#define App_Version [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]


#pragma mark 设备类型判断------------------
/// 是否是iphoneX
//#define iPhoneX ([[UIScreen mainScreen] bounds].size.height >= 812)
// 是否模拟器
#define kIsSimulator                    (NSNotFound != [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location)

//判断是否是ipad
#define kIsPad ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
//判断iPhone6 6s 7系列
#define kiPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) && !kIsPad : NO)
//判断iPhone6p 6sp 7p系列
#define kiPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) && !kIsPad : NO)
//判断iPhoneX，Xs（iPhoneX，iPhoneXs）
#define IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) && !kIsPad : NO)
//判断iPhoneXr
#define IS_IPHONE_Xr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) && !kIsPad : NO)
//判断iPhoneXsMax
#define IS_IPHONE_Xs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size)&& !kIsPad : NO)


#pragma mark  设备类型判断------------------
#define kIS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
#define IS_SIMULATOR ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"])
/*
 *@bref  设备系统判断
 */
#define kVersion                        [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
#define kIsIOS9                         ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)


/**
 *  屏幕各个部分尺寸
 */

#define kFontSize(size)      [UIFont systemFontOfSize:Ratio(size)]
#define kBoldFontSize(size)      [UIFont boldSystemFontOfSize:Ratio(size)]



/******************** 屏幕UI ******************************/
#define kScreenWidth        ([UIScreen mainScreen].bounds).size.width //应用程序的宽度
#define kScreenHeight       ([UIScreen mainScreen].bounds).size.height //应用程序的高度

#define kViewSizeWidth      (self.view.frame.size.width)
#define kViewSizeHeight     (self.view.frame.size.height)

#define kLineHeight                    1.0
#define kViewControllerHeadHeight       (kStatusBarHeight+44)
#define kAutoScaleSize(size,designScreenSize)      (kScreenWidth * size / designScreenSize)


/// 是否刘海屏
#define kIsBangsScreen ({\
    BOOL isBangsScreen = NO; \
    if (@available(iOS 11.0, *)) { \
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject]; \
    isBangsScreen = window.safeAreaInsets.bottom > 0; \
    } \
    isBangsScreen; \
})

// 状态栏高度
#define kStatusBarHeight  [UIApplication sharedApplication].statusBarFrame.size.height
#define kNavBarHeight [UIViewController currentNavigationController].navigationBar.frame.size.height
// 导航栏高度
#define kNavigationBarHeight (kStatusBarHeight + kNavBarHeight)
#define kSafeAreaTopHeight (kStatusBarHeight + kNavBarHeight))
#define kSafeAreaBottomHeight (kIsBangsScreen ? 34 : 0)
#define kAdaptBottomH(x) ((kStatusBarHeight > 20.0f) ? (x + 25) : x)


#define kFont(x) [UIFont systemFontOfSize:x]//正常文字大小 常规
#define kFontRatio(x) [UIFont systemFontOfSize:Ratio(x)]//等比文字大小

/**************** 屏幕UI end *********************/

#define Ratio(x) (int)((kScreenWidth/(kIsPad?1024:414)) * (x))


//#define Ratio(x) (int)((kScreenWidth/414) * (x))  //6s:375   xr/11:414

#define kCGSizeMake(x,y) CGSizeMake(Ratio(x),Ratio(y))
/// cell线高度
#define kCellLineHeight 0.5
/// 分割线
#define SINGLE_LINE_WIDTH           (1 / [UIScreen mainScreen].scale)
#define SINGLE_LINE_ADJUST_OFFSET   ((1 / [UIScreen mainScreen].scale) / 2)


// View 圆角和加边框
#define ViewBorderRadius(View, Radius, Width, Color)\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

// 禁止Layer隐式动画
#ifndef tj_disable_animations
#define tj_disable_animations(block)\
[CATransaction begin];\
[CATransaction setDisableActions:YES];\
block();\
[CATransaction commit];
#endif

/** 应用中常用颜色 */
//rgb颜色转换（16进制->10进制）
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define RGBA(r,g,b,a)               [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:a]
#define RGB(r, g, b) [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1.0f]
#define ColorWithString(string) [UIColor colorWithString:(NSString *)string]


#define kMapDateFormat @"yyyyMMdd"     //yyyy-MM-dd HH:mm:ss
#define kSearchRecord @"kSearchRecord"
#define kHTTask @"HTTask"


#pragma mark 用户偏好的key
#define kUserDefaultFirstLaunch               @"kUserDefaultFirstLaunch" //YES非第一次 NO第一次


//主要单例
#define UserDefaults [NSUserDefaults standardUserDefaults]
#define NotificationCenter [NSNotificationCenter defaultCenter]
#define SharedApplication [UIApplication sharedApplication]
#define Bundle [NSBundle mainBundle]
#define MainScreen [UIScreen mainScreen]

// 获取系统时间戳
#define kGetCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]


//解决循环引用问题
#ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
#endif


#ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
#endif


// 防止多次调用
#define kPreventRepeatClickTime(_seconds_) \
static BOOL shouldPrevent; \
if (shouldPrevent) return; \
shouldPrevent = YES; \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((_seconds_) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ \
shouldPrevent = NO; \
}); \


#endif /* WBCommonDefine_h */
